﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Doi_mat_khau : System.Web.UI.Page
{
    private string Connect = ConfigurationManager.ConnectionStrings["ketnoi"].ConnectionString;
    SqlConnection con;
    SqlCommand cmd;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"].ToString() == "")
        {
            Response.Redirect("DangNhap.aspx");
        }
        else
        {
            lbluser.Text = "" + Session["username"].ToString();
            
        }
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "select * from Dang_nhap where tendangnhap='" + Session["username"].ToString() + "'";
        con = new SqlConnection(Connect);
        con.Open();
        cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = sql;
        SqlDataReader reader=cmd.ExecuteReader();
        while (reader.Read())
        {
            string strMatkhau;
            strMatkhau = reader[1].ToString();
            if (strMatkhau !=txtmatkhaucu.Text)
            {
                lblinfo.Text = "Mật khẩu củ không chính xác";
            }
            else
            {
                string sql1 = "update Dang_Nhap set MatKhau='" + txtmatkhaumoi.Text + "' where TenDangNhap='" + Session["username"].ToString() + "'";
                con = new SqlConnection(Connect);
                try
                {
                    con.Open();
                    cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = sql1;
                    cmd.CommandType = CommandType.Text;
                    int j;
                    j = cmd.ExecuteNonQuery();
                    if (j > 0)
                    {
                        Response.Redirect("Dang_Nhap_Thanh_Cong.aspx");
                    }
                    else
                    {
                        lblinfo.Text = "Đổi mật khẩu thất bại";
                    }
                }
                catch
                {
                    lblinfo.Text = "Lỗi kết nối";
                }
                con.Dispose();
                con.Close();
            }
        }
        con.Dispose();
        con.Close();
    }
    protected void txtmatkhaucu_TextChanged(object sender, EventArgs e)
    {
       
    }
}
